<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>entry.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_fs_entry.js.html">entry.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Mock filesystem objects. These are all in the same file to
<a name="line17"></a> * avoid circular dependency issues.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.testing.fs.DirectoryEntry&#39;);
<a name="line22"></a>goog.provide(&#39;goog.testing.fs.Entry&#39;);
<a name="line23"></a>goog.provide(&#39;goog.testing.fs.FileEntry&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line26"></a>goog.require(&#39;goog.array&#39;);
<a name="line27"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line28"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line29"></a>goog.require(&#39;goog.fs.DirectoryEntry&#39;);
<a name="line30"></a>goog.require(&#39;goog.fs.DirectoryEntryImpl&#39;);
<a name="line31"></a>goog.require(&#39;goog.fs.Entry&#39;);
<a name="line32"></a>goog.require(&#39;goog.fs.Error&#39;);
<a name="line33"></a>goog.require(&#39;goog.fs.FileEntry&#39;);
<a name="line34"></a>goog.require(&#39;goog.functions&#39;);
<a name="line35"></a>goog.require(&#39;goog.object&#39;);
<a name="line36"></a>goog.require(&#39;goog.string&#39;);
<a name="line37"></a>goog.require(&#39;goog.testing.fs.File&#39;);
<a name="line38"></a>goog.require(&#39;goog.testing.fs.FileWriter&#39;);
<a name="line39"></a>
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>/**
<a name="line43"></a> * A mock filesystem entry object.
<a name="line44"></a> *
<a name="line45"></a> * @param {!goog.testing.fs.FileSystem} fs The filesystem containing this entry.
<a name="line46"></a> * @param {!goog.testing.fs.DirectoryEntry} parent The directory entry directly
<a name="line47"></a> *     containing this entry.
<a name="line48"></a> * @param {string} name The name of this entry.
<a name="line49"></a> * @constructor
<a name="line50"></a> * @implements {goog.fs.Entry}
<a name="line51"></a> */
<a name="line52"></a>goog.testing.fs.Entry = function(fs, parent, name) {
<a name="line53"></a>  /**
<a name="line54"></a>   * This entry&#39;s filesystem.
<a name="line55"></a>   * @type {!goog.testing.fs.FileSystem}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.fs_ = fs;
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * The name of this entry.
<a name="line62"></a>   * @type {string}
<a name="line63"></a>   * @private
<a name="line64"></a>   */
<a name="line65"></a>  this.name_ = name;
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * The parent of this entry.
<a name="line69"></a>   * @type {!goog.testing.fs.DirectoryEntry}
<a name="line70"></a>   */
<a name="line71"></a>  this.parent = parent;
<a name="line72"></a>};
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * Whether or not this entry has been deleted.
<a name="line77"></a> * @type {boolean}
<a name="line78"></a> */
<a name="line79"></a>goog.testing.fs.Entry.prototype.deleted = false;
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/** @override */
<a name="line83"></a>goog.testing.fs.Entry.prototype.isFile = goog.abstractMethod;
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/** @override */
<a name="line87"></a>goog.testing.fs.Entry.prototype.isDirectory = goog.abstractMethod;
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/** @override */
<a name="line91"></a>goog.testing.fs.Entry.prototype.getName = function() {
<a name="line92"></a>  return this.name_;
<a name="line93"></a>};
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/** @override */
<a name="line97"></a>goog.testing.fs.Entry.prototype.getFullPath = function() {
<a name="line98"></a>  if (this.getName() == &#39;&#39; || this.parent.getName() == &#39;&#39;) {
<a name="line99"></a>    // The root directory has an empty name
<a name="line100"></a>    return &#39;/&#39; + this.name_;
<a name="line101"></a>  } else {
<a name="line102"></a>    return this.parent.getFullPath() + &#39;/&#39; + this.name_;
<a name="line103"></a>  }
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * @return {!goog.testing.fs.FileSystem}
<a name="line109"></a> * @override
<a name="line110"></a> */
<a name="line111"></a>goog.testing.fs.Entry.prototype.getFileSystem = function() {
<a name="line112"></a>  return this.fs_;
<a name="line113"></a>};
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/** @override */
<a name="line117"></a>goog.testing.fs.Entry.prototype.getLastModified = goog.abstractMethod;
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/** @override */
<a name="line121"></a>goog.testing.fs.Entry.prototype.getMetadata = goog.abstractMethod;
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/** @override */
<a name="line125"></a>goog.testing.fs.Entry.prototype.moveTo = function(parent, opt_newName) {
<a name="line126"></a>  var msg = &#39;moving &#39; + this.getFullPath() + &#39; into &#39; + parent.getFullPath() +
<a name="line127"></a>      (opt_newName ? &#39;, renaming to &#39; + opt_newName : &#39;&#39;);
<a name="line128"></a>  var newFile;
<a name="line129"></a>  return this.checkNotDeleted(msg).
<a name="line130"></a>      addCallback(function() { return this.copyTo(parent, opt_newName); }).
<a name="line131"></a>      addCallback(function(file) {
<a name="line132"></a>        newFile = file;
<a name="line133"></a>        return this.remove();
<a name="line134"></a>      }).addCallback(function() { return newFile; });
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/** @override */
<a name="line139"></a>goog.testing.fs.Entry.prototype.copyTo = function(parent, opt_newName) {
<a name="line140"></a>  goog.asserts.assert(parent instanceof goog.testing.fs.DirectoryEntry);
<a name="line141"></a>  var msg = &#39;copying &#39; + this.getFullPath() + &#39; into &#39; + parent.getFullPath() +
<a name="line142"></a>      (opt_newName ? &#39;, renaming to &#39; + opt_newName : &#39;&#39;);
<a name="line143"></a>  return this.checkNotDeleted(msg).addCallback(function() {
<a name="line144"></a>    var name = opt_newName || this.getName();
<a name="line145"></a>    var entry = this.clone();
<a name="line146"></a>    parent.children[name] = entry;
<a name="line147"></a>    parent.lastModifiedTimestamp_ = goog.now();
<a name="line148"></a>    entry.name_ = name;
<a name="line149"></a>    entry.parent = parent;
<a name="line150"></a>    return entry;
<a name="line151"></a>  });
<a name="line152"></a>};
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>/**
<a name="line156"></a> * @return {!goog.testing.fs.Entry} A shallow copy of this entry object.
<a name="line157"></a> */
<a name="line158"></a>goog.testing.fs.Entry.prototype.clone = goog.abstractMethod;
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>/** @override */
<a name="line162"></a>goog.testing.fs.Entry.prototype.toUrl = function(opt_mimetype) {
<a name="line163"></a>  return &#39;fakefilesystem:&#39; + this.getFullPath();
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/** @override */
<a name="line168"></a>goog.testing.fs.Entry.prototype.toUri = goog.testing.fs.Entry.prototype.toUrl;
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/** @override */
<a name="line172"></a>goog.testing.fs.Entry.prototype.wrapEntry = goog.abstractMethod;
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/** @override */
<a name="line176"></a>goog.testing.fs.Entry.prototype.remove = function() {
<a name="line177"></a>  var msg = &#39;removing &#39; + this.getFullPath();
<a name="line178"></a>  return this.checkNotDeleted(msg).addCallback(function() {
<a name="line179"></a>    delete this.parent.children[this.getName()];
<a name="line180"></a>    this.parent.lastModifiedTimestamp_ = goog.now();
<a name="line181"></a>    this.deleted = true;
<a name="line182"></a>    return;
<a name="line183"></a>  });
<a name="line184"></a>};
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>/** @override */
<a name="line188"></a>goog.testing.fs.Entry.prototype.getParent = function() {
<a name="line189"></a>  var msg = &#39;getting parent of &#39; + this.getFullPath();
<a name="line190"></a>  return this.checkNotDeleted(msg).
<a name="line191"></a>      addCallback(function() { return this.parent; });
<a name="line192"></a>};
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * Return a deferred that will call its errback if this entry has been deleted.
<a name="line197"></a> * In addition, the deferred will only run after a timeout of 0, and all its
<a name="line198"></a> * callbacks will run with the entry as &quot;this&quot;.
<a name="line199"></a> *
<a name="line200"></a> * @param {string} action The name of the action being performed. For error
<a name="line201"></a> *     reporting.
<a name="line202"></a> * @return {!goog.async.Deferred} The deferred that will be called after a
<a name="line203"></a> *     timeout of 0.
<a name="line204"></a> * @protected
<a name="line205"></a> */
<a name="line206"></a>goog.testing.fs.Entry.prototype.checkNotDeleted = function(action) {
<a name="line207"></a>  var d = new goog.async.Deferred(undefined, this);
<a name="line208"></a>  goog.Timer.callOnce(function() {
<a name="line209"></a>    if (this.deleted) {
<a name="line210"></a>      var err = new goog.fs.Error(
<a name="line211"></a>          /** @type {!FileError} */ ({&#39;name&#39;: &#39;NotFoundError&#39;}),
<a name="line212"></a>          action);
<a name="line213"></a>      d.errback(err);
<a name="line214"></a>    } else {
<a name="line215"></a>      d.callback();
<a name="line216"></a>    }
<a name="line217"></a>  }, 0, this);
<a name="line218"></a>  return d;
<a name="line219"></a>};
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>
<a name="line223"></a>/**
<a name="line224"></a> * A mock directory entry object.
<a name="line225"></a> *
<a name="line226"></a> * @param {!goog.testing.fs.FileSystem} fs The filesystem containing this entry.
<a name="line227"></a> * @param {goog.testing.fs.DirectoryEntry} parent The directory entry directly
<a name="line228"></a> *     containing this entry. If this is null, that means this is the root
<a name="line229"></a> *     directory and so is its own parent.
<a name="line230"></a> * @param {string} name The name of this entry.
<a name="line231"></a> * @param {!Object.&lt;!goog.testing.fs.Entry&gt;} children The map of child names to
<a name="line232"></a> *     entry objects.
<a name="line233"></a> * @constructor
<a name="line234"></a> * @extends {goog.testing.fs.Entry}
<a name="line235"></a> * @implements {goog.fs.DirectoryEntry}
<a name="line236"></a> * @final
<a name="line237"></a> */
<a name="line238"></a>goog.testing.fs.DirectoryEntry = function(fs, parent, name, children) {
<a name="line239"></a>  goog.testing.fs.DirectoryEntry.base(
<a name="line240"></a>      this, &#39;constructor&#39;, fs, parent || this, name);
<a name="line241"></a>
<a name="line242"></a>  /**
<a name="line243"></a>   * The map of child names to entry objects.
<a name="line244"></a>   * @type {!Object.&lt;!goog.testing.fs.Entry&gt;}
<a name="line245"></a>   */
<a name="line246"></a>  this.children = children;
<a name="line247"></a>
<a name="line248"></a>  /**
<a name="line249"></a>   * The modification time of the directory. Measured using goog.now, which may
<a name="line250"></a>   * be overridden with mock time providers.
<a name="line251"></a>   * @type {number}
<a name="line252"></a>   * @private
<a name="line253"></a>   */
<a name="line254"></a>  this.lastModifiedTimestamp_ = goog.now();
<a name="line255"></a>};
<a name="line256"></a>goog.inherits(goog.testing.fs.DirectoryEntry, goog.testing.fs.Entry);
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>/**
<a name="line260"></a> * Constructs and returns the metadata object for this entry.
<a name="line261"></a> * @return {{modificationTime: Date}} The metadata object.
<a name="line262"></a> * @private
<a name="line263"></a> */
<a name="line264"></a>goog.testing.fs.DirectoryEntry.prototype.getMetadata_ = function() {
<a name="line265"></a>  return {
<a name="line266"></a>    &#39;modificationTime&#39;: new Date(this.lastModifiedTimestamp_)
<a name="line267"></a>  };
<a name="line268"></a>};
<a name="line269"></a>
<a name="line270"></a>
<a name="line271"></a>/** @override */
<a name="line272"></a>goog.testing.fs.DirectoryEntry.prototype.isFile = function() {
<a name="line273"></a>  return false;
<a name="line274"></a>};
<a name="line275"></a>
<a name="line276"></a>
<a name="line277"></a>/** @override */
<a name="line278"></a>goog.testing.fs.DirectoryEntry.prototype.isDirectory = function() {
<a name="line279"></a>  return true;
<a name="line280"></a>};
<a name="line281"></a>
<a name="line282"></a>
<a name="line283"></a>/** @override */
<a name="line284"></a>goog.testing.fs.DirectoryEntry.prototype.getLastModified = function() {
<a name="line285"></a>  var msg = &#39;reading last modified date for &#39; + this.getFullPath();
<a name="line286"></a>  return this.checkNotDeleted(msg).
<a name="line287"></a>      addCallback(function() {return new Date(this.lastModifiedTimestamp_)});
<a name="line288"></a>};
<a name="line289"></a>
<a name="line290"></a>
<a name="line291"></a>/** @override */
<a name="line292"></a>goog.testing.fs.DirectoryEntry.prototype.getMetadata = function() {
<a name="line293"></a>  var msg = &#39;reading metadata for &#39; + this.getFullPath();
<a name="line294"></a>  return this.checkNotDeleted(msg).
<a name="line295"></a>      addCallback(function() {return this.getMetadata_()});
<a name="line296"></a>};
<a name="line297"></a>
<a name="line298"></a>
<a name="line299"></a>/** @override */
<a name="line300"></a>goog.testing.fs.DirectoryEntry.prototype.clone = function() {
<a name="line301"></a>  return new goog.testing.fs.DirectoryEntry(
<a name="line302"></a>      this.getFileSystem(), this.parent, this.getName(), this.children);
<a name="line303"></a>};
<a name="line304"></a>
<a name="line305"></a>
<a name="line306"></a>/** @override */
<a name="line307"></a>goog.testing.fs.DirectoryEntry.prototype.remove = function() {
<a name="line308"></a>  if (!goog.object.isEmpty(this.children)) {
<a name="line309"></a>    var d = new goog.async.Deferred();
<a name="line310"></a>    goog.Timer.callOnce(function() {
<a name="line311"></a>      d.errback(new goog.fs.Error(
<a name="line312"></a>          /** @type {!FileError} */ ({&#39;name&#39;: &#39;InvalidModificationError&#39;}),
<a name="line313"></a>          &#39;removing &#39; + this.getFullPath()));
<a name="line314"></a>    }, 0, this);
<a name="line315"></a>    return d;
<a name="line316"></a>  } else if (this != this.getFileSystem().getRoot()) {
<a name="line317"></a>    return goog.testing.fs.DirectoryEntry.base(this, &#39;remove&#39;);
<a name="line318"></a>  } else {
<a name="line319"></a>    // Root directory, do nothing.
<a name="line320"></a>    return goog.async.Deferred.succeed();
<a name="line321"></a>  }
<a name="line322"></a>};
<a name="line323"></a>
<a name="line324"></a>
<a name="line325"></a>/** @override */
<a name="line326"></a>goog.testing.fs.DirectoryEntry.prototype.getFile = function(
<a name="line327"></a>    path, opt_behavior) {
<a name="line328"></a>  var msg = &#39;loading file &#39; + path + &#39; from &#39; + this.getFullPath();
<a name="line329"></a>  opt_behavior = opt_behavior || goog.fs.DirectoryEntry.Behavior.DEFAULT;
<a name="line330"></a>  return this.checkNotDeleted(msg).addCallback(function() {
<a name="line331"></a>    try {
<a name="line332"></a>      return goog.async.Deferred.succeed(this.getFileSync(path, opt_behavior));
<a name="line333"></a>    } catch (e) {
<a name="line334"></a>      return goog.async.Deferred.fail(e);
<a name="line335"></a>    }
<a name="line336"></a>  });
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>
<a name="line340"></a>/** @override */
<a name="line341"></a>goog.testing.fs.DirectoryEntry.prototype.getDirectory = function(
<a name="line342"></a>    path, opt_behavior) {
<a name="line343"></a>  var msg = &#39;loading directory &#39; + path + &#39; from &#39; + this.getFullPath();
<a name="line344"></a>  opt_behavior = opt_behavior || goog.fs.DirectoryEntry.Behavior.DEFAULT;
<a name="line345"></a>  return this.checkNotDeleted(msg).addCallback(function() {
<a name="line346"></a>    try {
<a name="line347"></a>      return goog.async.Deferred.succeed(
<a name="line348"></a>          this.getDirectorySync(path, opt_behavior));
<a name="line349"></a>    } catch (e) {
<a name="line350"></a>      return goog.async.Deferred.fail(e);
<a name="line351"></a>    }
<a name="line352"></a>  });
<a name="line353"></a>};
<a name="line354"></a>
<a name="line355"></a>
<a name="line356"></a>/**
<a name="line357"></a> * Get a file entry synchronously, without waiting for a Deferred to resolve.
<a name="line358"></a> *
<a name="line359"></a> * @param {string} path The path to the file, relative to this directory.
<a name="line360"></a> * @param {goog.fs.DirectoryEntry.Behavior=} opt_behavior The behavior for
<a name="line361"></a> *     loading the file.
<a name="line362"></a> * @param {string=} opt_data The string data encapsulated by the blob.
<a name="line363"></a> * @param {string=} opt_type The mime type of the blob.
<a name="line364"></a> * @return {!goog.testing.fs.FileEntry} The loaded file.
<a name="line365"></a> */
<a name="line366"></a>goog.testing.fs.DirectoryEntry.prototype.getFileSync = function(
<a name="line367"></a>    path, opt_behavior, opt_data, opt_type) {
<a name="line368"></a>  opt_behavior = opt_behavior || goog.fs.DirectoryEntry.Behavior.DEFAULT;
<a name="line369"></a>  return (/** @type {!goog.testing.fs.FileEntry} */ (this.getEntry_(
<a name="line370"></a>      path, opt_behavior, true /* isFile */,
<a name="line371"></a>      goog.bind(function(parent, name) {
<a name="line372"></a>        return new goog.testing.fs.FileEntry(
<a name="line373"></a>            this.getFileSystem(), parent, name,
<a name="line374"></a>            goog.isDef(opt_data) ? opt_data : &#39;&#39;, opt_type);
<a name="line375"></a>      }, this))));
<a name="line376"></a>};
<a name="line377"></a>
<a name="line378"></a>
<a name="line379"></a>/**
<a name="line380"></a> * Creates a file synchronously. This is a shorthand for getFileSync, useful for
<a name="line381"></a> * setting up tests.
<a name="line382"></a> *
<a name="line383"></a> * @param {string} path The path to the file, relative to this directory.
<a name="line384"></a> * @return {!goog.testing.fs.FileEntry} The created file.
<a name="line385"></a> */
<a name="line386"></a>goog.testing.fs.DirectoryEntry.prototype.createFileSync = function(path) {
<a name="line387"></a>  return this.getFileSync(path, goog.fs.DirectoryEntry.Behavior.CREATE);
<a name="line388"></a>};
<a name="line389"></a>
<a name="line390"></a>
<a name="line391"></a>/**
<a name="line392"></a> * Get a directory synchronously, without waiting for a Deferred to resolve.
<a name="line393"></a> *
<a name="line394"></a> * @param {string} path The path to the directory, relative to this one.
<a name="line395"></a> * @param {goog.fs.DirectoryEntry.Behavior=} opt_behavior The behavior for
<a name="line396"></a> *     loading the directory.
<a name="line397"></a> * @return {!goog.testing.fs.DirectoryEntry} The loaded directory.
<a name="line398"></a> */
<a name="line399"></a>goog.testing.fs.DirectoryEntry.prototype.getDirectorySync = function(
<a name="line400"></a>    path, opt_behavior) {
<a name="line401"></a>  opt_behavior = opt_behavior || goog.fs.DirectoryEntry.Behavior.DEFAULT;
<a name="line402"></a>  return (/** @type {!goog.testing.fs.DirectoryEntry} */ (this.getEntry_(
<a name="line403"></a>      path, opt_behavior, false /* isFile */,
<a name="line404"></a>      goog.bind(function(parent, name) {
<a name="line405"></a>        return new goog.testing.fs.DirectoryEntry(
<a name="line406"></a>            this.getFileSystem(), parent, name, {});
<a name="line407"></a>      }, this))));
<a name="line408"></a>};
<a name="line409"></a>
<a name="line410"></a>
<a name="line411"></a>/**
<a name="line412"></a> * Creates a directory synchronously. This is a shorthand for getFileSync,
<a name="line413"></a> * useful for setting up tests.
<a name="line414"></a> *
<a name="line415"></a> * @param {string} path The path to the directory, relative to this directory.
<a name="line416"></a> * @return {!goog.testing.fs.DirectoryEntry} The created directory.
<a name="line417"></a> */
<a name="line418"></a>goog.testing.fs.DirectoryEntry.prototype.createDirectorySync = function(path) {
<a name="line419"></a>  return this.getDirectorySync(path, goog.fs.DirectoryEntry.Behavior.CREATE);
<a name="line420"></a>};
<a name="line421"></a>
<a name="line422"></a>
<a name="line423"></a>/**
<a name="line424"></a> * Get a file or directory entry from a path. This handles parsing the path for
<a name="line425"></a> * subdirectories and throwing appropriate errors should something go wrong.
<a name="line426"></a> *
<a name="line427"></a> * @param {string} path The path to the entry, relative to this directory.
<a name="line428"></a> * @param {goog.fs.DirectoryEntry.Behavior} behavior The behavior for loading
<a name="line429"></a> *     the entry.
<a name="line430"></a> * @param {boolean} isFile Whether a file or directory is being loaded.
<a name="line431"></a> * @param {function(!goog.testing.fs.DirectoryEntry, string) :
<a name="line432"></a> *             !goog.testing.fs.Entry} createFn
<a name="line433"></a> *     The function for creating the entry if it doesn&#39;t yet exist. This is
<a name="line434"></a> *     passed the parent entry and the name of the new entry.
<a name="line435"></a> * @return {!goog.testing.fs.Entry} The loaded entry.
<a name="line436"></a> * @private
<a name="line437"></a> */
<a name="line438"></a>goog.testing.fs.DirectoryEntry.prototype.getEntry_ = function(
<a name="line439"></a>    path, behavior, isFile, createFn) {
<a name="line440"></a>  // Filter out leading, trailing, and duplicate slashes.
<a name="line441"></a>  var components = goog.array.filter(path.split(&#39;/&#39;), goog.functions.identity);
<a name="line442"></a>
<a name="line443"></a>  var basename = /** @type {string} */ (goog.array.peek(components)) || &#39;&#39;;
<a name="line444"></a>  var dir = goog.string.startsWith(path, &#39;/&#39;) ?
<a name="line445"></a>      this.getFileSystem().getRoot() : this;
<a name="line446"></a>
<a name="line447"></a>  goog.array.forEach(components.slice(0, -1), function(p) {
<a name="line448"></a>    var subdir = dir.children[p];
<a name="line449"></a>    if (!subdir) {
<a name="line450"></a>      throw new goog.fs.Error(
<a name="line451"></a>          /** @type {!FileError} */ ({&#39;name&#39;: &#39;NotFoundError&#39;}),
<a name="line452"></a>          &#39;loading &#39; + path + &#39; from &#39; + this.getFullPath() + &#39; (directory &#39; +
<a name="line453"></a>          dir.getFullPath() + &#39;/&#39; + p + &#39;)&#39;);
<a name="line454"></a>    }
<a name="line455"></a>    dir = subdir;
<a name="line456"></a>  }, this);
<a name="line457"></a>
<a name="line458"></a>  // If there is no basename, the path must resolve to the root directory.
<a name="line459"></a>  var entry = basename ? dir.children[basename] : dir;
<a name="line460"></a>
<a name="line461"></a>  if (!entry) {
<a name="line462"></a>    if (behavior == goog.fs.DirectoryEntry.Behavior.DEFAULT) {
<a name="line463"></a>      throw new goog.fs.Error(
<a name="line464"></a>          /** @type {!FileError} */ ({&#39;name&#39;: &#39;NotFoundError&#39;}),
<a name="line465"></a>          &#39;loading &#39; + path + &#39; from &#39; + this.getFullPath());
<a name="line466"></a>    } else {
<a name="line467"></a>      goog.asserts.assert(
<a name="line468"></a>          behavior == goog.fs.DirectoryEntry.Behavior.CREATE ||
<a name="line469"></a>          behavior == goog.fs.DirectoryEntry.Behavior.CREATE_EXCLUSIVE);
<a name="line470"></a>      entry = createFn(dir, basename);
<a name="line471"></a>      dir.children[basename] = entry;
<a name="line472"></a>      this.lastModifiedTimestamp_ = goog.now();
<a name="line473"></a>      return entry;
<a name="line474"></a>    }
<a name="line475"></a>  } else if (behavior == goog.fs.DirectoryEntry.Behavior.CREATE_EXCLUSIVE) {
<a name="line476"></a>    throw new goog.fs.Error(
<a name="line477"></a>        /** @type {!FileError} */ ({&#39;name&#39;: &#39;InvalidModificationError&#39;}),
<a name="line478"></a>        &#39;loading &#39; + path + &#39; from &#39; + this.getFullPath());
<a name="line479"></a>  } else if (entry.isFile() != isFile) {
<a name="line480"></a>    throw new goog.fs.Error(
<a name="line481"></a>        /** @type {!FileError} */ ({&#39;name&#39;: &#39;TypeMismatchError&#39;}),
<a name="line482"></a>        &#39;loading &#39; + path + &#39; from &#39; + this.getFullPath());
<a name="line483"></a>  } else {
<a name="line484"></a>    if (behavior == goog.fs.DirectoryEntry.Behavior.CREATE) {
<a name="line485"></a>      this.lastModifiedTimestamp_ = goog.now();
<a name="line486"></a>    }
<a name="line487"></a>    return entry;
<a name="line488"></a>  }
<a name="line489"></a>};
<a name="line490"></a>
<a name="line491"></a>
<a name="line492"></a>/**
<a name="line493"></a> * Returns whether this directory has a child with the given name.
<a name="line494"></a> *
<a name="line495"></a> * @param {string} name The name of the entry to check for.
<a name="line496"></a> * @return {boolean} Whether or not this has a child with the given name.
<a name="line497"></a> */
<a name="line498"></a>goog.testing.fs.DirectoryEntry.prototype.hasChild = function(name) {
<a name="line499"></a>  return name in this.children;
<a name="line500"></a>};
<a name="line501"></a>
<a name="line502"></a>
<a name="line503"></a>/** @override */
<a name="line504"></a>goog.testing.fs.DirectoryEntry.prototype.removeRecursively = function() {
<a name="line505"></a>  var msg = &#39;removing &#39; + this.getFullPath() + &#39; recursively&#39;;
<a name="line506"></a>  return this.checkNotDeleted(msg).addCallback(function() {
<a name="line507"></a>    var d = goog.async.Deferred.succeed(null);
<a name="line508"></a>    goog.object.forEach(this.children, function(child) {
<a name="line509"></a>      d.awaitDeferred(
<a name="line510"></a>          child.isDirectory() ? child.removeRecursively() : child.remove());
<a name="line511"></a>    });
<a name="line512"></a>    d.addCallback(function() { return this.remove(); }, this);
<a name="line513"></a>    return d;
<a name="line514"></a>  });
<a name="line515"></a>};
<a name="line516"></a>
<a name="line517"></a>
<a name="line518"></a>/** @override */
<a name="line519"></a>goog.testing.fs.DirectoryEntry.prototype.listDirectory = function() {
<a name="line520"></a>  var msg = &#39;listing &#39; + this.getFullPath();
<a name="line521"></a>  return this.checkNotDeleted(msg).addCallback(function() {
<a name="line522"></a>    return goog.object.getValues(this.children);
<a name="line523"></a>  });
<a name="line524"></a>};
<a name="line525"></a>
<a name="line526"></a>
<a name="line527"></a>/** @override */
<a name="line528"></a>goog.testing.fs.DirectoryEntry.prototype.createPath =
<a name="line529"></a>    // This isn&#39;t really type-safe.
<a name="line530"></a>    /** @type {!Function} */ (goog.fs.DirectoryEntryImpl.prototype.createPath);
<a name="line531"></a>
<a name="line532"></a>
<a name="line533"></a>
<a name="line534"></a>/**
<a name="line535"></a> * A mock file entry object.
<a name="line536"></a> *
<a name="line537"></a> * @param {!goog.testing.fs.FileSystem} fs The filesystem containing this entry.
<a name="line538"></a> * @param {!goog.testing.fs.DirectoryEntry} parent The directory entry directly
<a name="line539"></a> *     containing this entry.
<a name="line540"></a> * @param {string} name The name of this entry.
<a name="line541"></a> * @param {string} data The data initially contained in the file.
<a name="line542"></a> * @param {string=} opt_type The mime type of the blob.
<a name="line543"></a> * @constructor
<a name="line544"></a> * @extends {goog.testing.fs.Entry}
<a name="line545"></a> * @implements {goog.fs.FileEntry}
<a name="line546"></a> * @final
<a name="line547"></a> */
<a name="line548"></a>goog.testing.fs.FileEntry = function(fs, parent, name, data, opt_type) {
<a name="line549"></a>  goog.testing.fs.FileEntry.base(this, &#39;constructor&#39;, fs, parent, name);
<a name="line550"></a>
<a name="line551"></a>  /**
<a name="line552"></a>   * The internal file blob referenced by this file entry.
<a name="line553"></a>   * @type {!goog.testing.fs.File}
<a name="line554"></a>   * @private
<a name="line555"></a>   */
<a name="line556"></a>  this.file_ =
<a name="line557"></a>      new goog.testing.fs.File(name, new Date(goog.now()), data, opt_type);
<a name="line558"></a>
<a name="line559"></a>  /**
<a name="line560"></a>   * The metadata for file.
<a name="line561"></a>   * @type {{modificationTime: Date}}
<a name="line562"></a>   * @private
<a name="line563"></a>   */
<a name="line564"></a>  this.metadata_ = {
<a name="line565"></a>    &#39;modificationTime&#39;: this.file_.lastModifiedDate
<a name="line566"></a>  };
<a name="line567"></a>};
<a name="line568"></a>goog.inherits(goog.testing.fs.FileEntry, goog.testing.fs.Entry);
<a name="line569"></a>
<a name="line570"></a>
<a name="line571"></a>/** @override */
<a name="line572"></a>goog.testing.fs.FileEntry.prototype.isFile = function() {
<a name="line573"></a>  return true;
<a name="line574"></a>};
<a name="line575"></a>
<a name="line576"></a>
<a name="line577"></a>/** @override */
<a name="line578"></a>goog.testing.fs.FileEntry.prototype.isDirectory = function() {
<a name="line579"></a>  return false;
<a name="line580"></a>};
<a name="line581"></a>
<a name="line582"></a>
<a name="line583"></a>/** @override */
<a name="line584"></a>goog.testing.fs.FileEntry.prototype.clone = function() {
<a name="line585"></a>  return new goog.testing.fs.FileEntry(
<a name="line586"></a>      this.getFileSystem(), this.parent,
<a name="line587"></a>      this.getName(), this.fileSync().toString());
<a name="line588"></a>};
<a name="line589"></a>
<a name="line590"></a>
<a name="line591"></a>/** @override */
<a name="line592"></a>goog.testing.fs.FileEntry.prototype.getLastModified = function() {
<a name="line593"></a>  return this.file().addCallback(function(file) {
<a name="line594"></a>    return file.lastModifiedDate;
<a name="line595"></a>  });
<a name="line596"></a>};
<a name="line597"></a>
<a name="line598"></a>
<a name="line599"></a>/** @override */
<a name="line600"></a>goog.testing.fs.FileEntry.prototype.getMetadata = function() {
<a name="line601"></a>  var msg = &#39;getting metadata for &#39; + this.getFullPath();
<a name="line602"></a>  return this.checkNotDeleted(msg).addCallback(function() {
<a name="line603"></a>    return this.metadata_;
<a name="line604"></a>  });
<a name="line605"></a>};
<a name="line606"></a>
<a name="line607"></a>
<a name="line608"></a>/** @override */
<a name="line609"></a>goog.testing.fs.FileEntry.prototype.createWriter = function() {
<a name="line610"></a>  var d = new goog.async.Deferred();
<a name="line611"></a>  goog.Timer.callOnce(
<a name="line612"></a>      goog.bind(d.callback, d, new goog.testing.fs.FileWriter(this)));
<a name="line613"></a>  return d;
<a name="line614"></a>};
<a name="line615"></a>
<a name="line616"></a>
<a name="line617"></a>/** @override */
<a name="line618"></a>goog.testing.fs.FileEntry.prototype.file = function() {
<a name="line619"></a>  var msg = &#39;getting file for &#39; + this.getFullPath();
<a name="line620"></a>  return this.checkNotDeleted(msg).addCallback(function() {
<a name="line621"></a>    return this.fileSync();
<a name="line622"></a>  });
<a name="line623"></a>};
<a name="line624"></a>
<a name="line625"></a>
<a name="line626"></a>/**
<a name="line627"></a> * Get the internal file representation synchronously, without waiting for a
<a name="line628"></a> * Deferred to resolve.
<a name="line629"></a> *
<a name="line630"></a> * @return {!goog.testing.fs.File} The internal file blob referenced by this
<a name="line631"></a> *     FileEntry.
<a name="line632"></a> */
<a name="line633"></a>goog.testing.fs.FileEntry.prototype.fileSync = function() {
<a name="line634"></a>  return this.file_;
<a name="line635"></a>};
</pre>


</body>
</html>
